package com.drug.service.impl;

import com.drug.domain.*;
import com.drug.mapper.*;
import com.drug.service.SelectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SelectServiceImpl implements SelectService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
    @Autowired
    private DrugInformationMapper drugInformationMapper;
    @Autowired
    private PodiPdMapper podiPdMapper;

    /**
     * 按采购单查询
     * @param map
     * @param page
     * @return
     */
    @Override
    public Result findcaigouadan(Map map, Integer page) {
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        //药品类别
        DrugCategory drugCategory = new DrugCategory();
        //供货商
        Supplier supplier = new Supplier();
        //采购单
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        //药品品目
        DrugAtegory drugAtegory = new DrugAtegory();
        //中间表
        PodiPd podiPd = new PodiPd();
        //根据医院名字查id
        Hospital hospital = new Hospital();
        // Integer hId = hospitalMapper.selectByName(hName);

        //采购单编号
        String ponumber = (String) map.get("ponumber");
        if (ponumber==null || ponumber.equals("")){
            ponumber=null;
        }else {
            purchaseOrder.setPoNumber(ponumber);
        }
        //采购单名称
        String poname = (String) map.get("poname");
        if (poname==null || poname.equals("")){
            poname=null;
        }else {
            purchaseOrder.setPoName(poname);
        }
        //采购状态
        String postate = (String) map.get("postate");
        if (poname==null || poname.equals("")){
            poname=null;
        }else {
            podiPd.setPodiState(postate);
        }
        //	采购医院
        String podihid = (String) map.get("podihid");
        if (podihid==null || podihid.equals("")){
            podihid=null;
        }else {
            Integer hId = hospitalMapper.selectByName(podihid);
            podiPd.setPodiHid(hId);
        }
        //流水号：
        String dinumber = (String) map.get("dinumber");
        if (dinumber==null || dinumber.equals("")){
            dinumber=null;
        }

        //通用名
        String cname = (String) map.get("cname");
        if (cname==null || cname.equals("")){
            cname=null;
        }
        String jixing1 = (String) map.get("jixing1");
        if (jixing1==null || jixing1.equals("")){
            jixing1=null;
        }
        String guige1 = (String) map.get("guige1");
        if (guige1==null || guige1.equals("")){
            guige1=null;
        }
        String danwei1 = (String) map.get("danwei1");
        if (danwei1==null || danwei1.equals("")){
            danwei1=null;
        }
        String ccc = (String) map.get("ccc");
        if (ccc==null || ccc.equals("")){
            ccc=null;
        }
        //药品类别企业
        String qiye = (String) map.get("qiye");
        if (qiye==null||qiye.equals("")){
            qiye=null;
        }else {
            drugAtegory.setDaMName(qiye);
        }
        //药品商品
        String shangpin = (String) map.get("shangpin");
        if (shangpin==null||shangpin.equals("")){
            shangpin=null;
        }else {
            drugAtegory.setDaName(shangpin);
        }
        String rukutime = (String) map.get("rukutime");
        if (rukutime==null || rukutime.equals("")){
            rukutime=null;
        }
        String rukutime1 = (String) map.get("rukutime1");
        if (rukutime1==null || rukutime1.equals("")){
            rukutime1=null;
        }
        String dcid1 = (String) map.get("dcid1");
        if (dcid1==null || dcid1.equals("0")){
            dcid1=null;
        }else {
            drugCategory.setDcName(dcid1);
        }
        DrugInformation drugInformation = new DrugInformation();
        drugInformation.setPurchaseOrder(purchaseOrder);
        drugInformation.setPodiPd(podiPd);
        drugInformation.setDiNumer(dinumber);
        drugInformation.setDiName(cname);
        drugInformation.setDiForm(jixing1);
        drugInformation.setDiSpecifications(guige1);
        drugInformation.setDiUnit(danwei1);
        drugInformation.setDiCc(ccc);


        List<DrugInformation> drugInformations = drugInformationMapper.selectaaa(drugInformation);
        PageInfo<DrugInformation> pageInfo = new PageInfo<>(drugInformations);
        result.setData(pageInfo);
        return result;
    }

    @Override
    public Result findghs(Map map, Integer page) {
        PageHelper.startPage(page,5);
        PodiPd podiPd = new PodiPd();
        String a = (String) map.get("a");
        if (a==null || a=="" || a=="0"){
            a=null;
        }else {
            podiPd.setPodiSurid(Integer.parseInt(a));
        }
        String b = (String) map.get("b");
        if (b==null || b==""){
            b=null;
        }else {
            podiPd.setPodiState(b);
        }
        List<PodiPd> aa = podiPdMapper.selectghs(podiPd);
        PageInfo<PodiPd> pageInfo = new PageInfo<>(aa);
        Result result = new Result<>();
        result.setData(pageInfo);
        return result;
    }

    @Override
    public Result findyiyuan(Map map, Integer page) {
        PageHelper.startPage(page,5);
        PodiPd podiPd = new PodiPd();
        String a = (String) map.get("a");
        if (a==null || a=="" || a=="0"){
            a=null;
        }else {
            podiPd.setPodiSurid(Integer.parseInt(a));
        }
        String b = (String) map.get("b");
        if (b==null || b==""){
            b=null;
        }else {
            podiPd.setPodiState(b);
        }
        List<PodiPd> aa = podiPdMapper.selectyiyuan(podiPd);
        PageInfo<PodiPd> pageInfo = new PageInfo<>(aa);
        Result result = new Result<>();
        result.setData(pageInfo);
        return result;
    }
}
